package com.xia.im.service.impl;


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xia.im.dao.SysUserDao;
import com.xia.im.entity.SysUser;
import com.xia.im.entity.resp.LoginVO;
import com.xia.im.entity.resp.UserRespVo;
import com.xia.im.exception.GlobalException;
import com.xia.im.service.SysUserService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;

@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserDao,SysUser> implements SysUserService {

    @Resource
    private SysUserDao sysUserDao;


    @Override
    public void addUser(SysUser sysUser) {
        sysUserDao.insert(sysUser);
    }

    @Override
    public LoginVO login(SysUser sysUser) {
        SysUser user = findUserByUserName(sysUser.getUsername());
        if(ObjectUtils.isEmpty(user)){
            throw new GlobalException("用户不存在");
        }
        //将用户信息封装
        UserRespVo userRespVo = new UserRespVo();
        BeanUtils.copyProperties(user, userRespVo);
        String json = JSON.toJSONString(userRespVo);
        LoginVO vo = new LoginVO();
        return vo;
    }

    @Override
    public SysUser findUserByUserName(String username) {
        LambdaQueryWrapper<SysUser> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysUser::getUsername,username);
        return sysUserDao.selectOne(wrapper);
    }
}
